﻿using M.WWHours.API.Command.WorkerListCommand;
using M.WWHours.API.Command.WWHoursCommand;
using M.WWHours.Domain.Dto;
using M.WWHours.Domain.Model;
using M_ApiResult;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace M.WWHours.API.Controllers
{
    /// <summary>
    /// 工人工时控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WWHoursController : ControllerBase
    {
        /// <summary>
        /// Mediator
        /// </summary>
        protected readonly IMediator _mediator;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="_mediator">Mediator</param>
        public WWHoursController(IMediator _mediator)
        {
            this._mediator = _mediator;
        }

        /// <summary>
        /// 新增工人工时数据
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> CreatedWWorkingHours(CreateWWHoursCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 工人工时列表分页展示
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<WWorkingHoursDto<WWorkingHours>>> ShowWWorkingHours([FromQuery] ShowWWHoursCommand command)
        {
            ApiResult<WWorkingHoursDto<WWorkingHours>> result = new ApiResult<WWorkingHoursDto<WWorkingHours>>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 删除工人工时信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DeleteWWorkingHours(DeletedWWHoursCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await _mediator.Send(command);

            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 反填工人工时信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<WWorkingHours>> ReverseFillingWWorkingHours([FromQuery] ReverseFillingWWHoursCommand command)
        {
            ApiResult<WWorkingHours> result = new ApiResult<WWorkingHours>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 修改工人工时信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateWWorkingHours(UpdateWWHoursCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 新增工人数据
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> CreatedWorkerList(CreateWorkerListCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 工人列表分页展示
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<WorkerListDto<WorkerList>>> ShowWorkerList([FromQuery] ShowWorkerListCommand command)
        {
            ApiResult<WorkerListDto<WorkerList>> result = new ApiResult<WorkerListDto<WorkerList>>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 删除工人信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DeleteWorkerList(DeletedWorkerListCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await _mediator.Send(command);

            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 反填工人信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<WorkerList>> ReverseFillingWorkerList([FromQuery] ReverseFilingWorkerListCommand command)
        {
            ApiResult<WorkerList> result = new ApiResult<WorkerList>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 修改工人信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateWorkerList(UpdateWorkerListCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 修改工人状态
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateWorkerListStatus(UpdateStatusWorkerListCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

    }
}
